//
//  RCServerPhotoCheckCell.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-29.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCServerPhotoCheckCell.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "UIImageView+WebCache.h"


@implementation RCServerPhotoCheckCell{
    UIImageView *_picImageView;
    UILabel     *_titleLabel;
    UILabel     *_dateTime;
    UILabel     *_typeLabel;
    
    
    int64_t     _reqID;
}

@synthesize picID = _picID;
@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self createPlaceHolder];
    }
    return self;
}


- (void) dealloc
{
    _delegate = nil;
    KF_RELEASE_SAFELY(_picID);
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    [super dealloc];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


- (void) createPlaceHolder
{
    _picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 115, 80)];
    [self.contentView addSubview:_picImageView];
    [_picImageView release];
    
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(145, 20, 50, 20)];
    [self.contentView addSubview:_titleLabel];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    _titleLabel.textColor = kNormalTextColor;
    [_titleLabel release];
    
    
    _dateTime = [[UILabel alloc] initWithFrame:CGRectMake(200, 25, 100, 15)];
    [self.contentView addSubview:_dateTime];
    _dateTime.backgroundColor = [UIColor clearColor];
    _dateTime.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _dateTime.textColor = kNormalTextColor;
    [_dateTime release];
    
    
    _typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(145, 45, 130, 15)];
    [self.contentView addSubview:_typeLabel];
    _typeLabel.backgroundColor = [UIColor clearColor];
    _typeLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _typeLabel.textColor = kNormalTextColor;
    [_typeLabel release];
    
    
    
    UIButton *passbtn = [[UIButton alloc] initWithFrame:CGRectMake(140, 70, 60, 27)];
    [self.contentView addSubview:passbtn];
    [passbtn addTarget:self action:@selector(passbtnClick) forControlEvents:UIControlEventTouchUpInside];
    [passbtn setTitle:@"通过" forState:UIControlStateNormal];
    passbtn.titleLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    [passbtn setBackgroundImage:[UIImage imageNamed:@"btn-bg-yellow"] forState:UIControlStateNormal];
    [passbtn release];
    
    
    UIButton *failbtn = [[UIButton alloc] initWithFrame:CGRectMake(205, 70, 60, 27)];
    [self.contentView addSubview:failbtn];
    [failbtn addTarget:self action:@selector(failbtnClick) forControlEvents:UIControlEventTouchUpInside];
    [failbtn setTitle:@"不通过" forState:UIControlStateNormal];
    failbtn.titleLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    [failbtn setBackgroundImage:[UIImage imageNamed:@"btn-bg-yellow"] forState:UIControlStateNormal];
    [failbtn release];
    
    
    UIView *horizonLine = [[UIView alloc]initWithFrame:CGRectMake(15, 109, 320-30, 1)];
    horizonLine.backgroundColor = [UIColor colorWithHex:0xffe6e6e6];
    [self.contentView addSubview:horizonLine];
    [horizonLine release];
    
}

- (void) bindData:(RCServerCheckPhotoItemModel *)item
{
    _titleLabel.text = item.name;
    _typeLabel.text = item.type;
    _dateTime.text = item.dateTime;
    [_picImageView setImageWithURL:[NSURL URLWithString:item.picUrl] placeholderImage:nil];
    self.picID = item.photoID;
}


- (void) failbtnClick
{

    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    _reqID = [[IManager sharedInstance] requestUpdatePic:self.picID withState:PicState_Fail withDelegate:self];
}


- (void) passbtnClick
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    _reqID = [[IManager sharedInstance] requestUpdatePic:self.picID withState:PicState_Pass withDelegate:self];

    
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if ([_delegate respondsToSelector:@selector(photoStateDidSubmitSuccess:withMsg:)]) {
        [_delegate photoStateDidSubmitSuccess:NO withMsg:@"提交失败"];
    }
}


- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (dataModel.errorNO == RCErrorTypeNoError) {
        if ([_delegate respondsToSelector:@selector(photoStateDidSubmitSuccess:withMsg:)]) {
            [_delegate photoStateDidSubmitSuccess:YES withMsg:@"提交成功"];
        }
    }else{
        if ([_delegate respondsToSelector:@selector(photoStateDidSubmitSuccess:withMsg:)]) {
            [_delegate photoStateDidSubmitSuccess:NO withMsg:dataModel.errorMsg];
        }
    }
    
}


@end
